<?php

namespace App\Dao;

use App\Exceptions\SysExceptions;
use App\Model\AddressModel;
use Illuminate\Support\Facades\DB;

class AddressDao extends BaseDao
{
    public static function get_address_builder_query($params = []): \Illuminate\Database\Eloquent\Builder
    {
        $query = AddressModel::query();
        $query->when(checkIsHasField($params,'user_id'),function ($query)use ($params){
            $query->where('user_id',$params['user_id']);
        });

        $query->when(checkIsHasField($params,'address_id'),function ($query)use ($params){
            $query->where('address_id',$params['address_id']);
        });

        return $query;
    }


    /**
     * @throws SysExceptions
     */
    public static function add_address($params = []){
        $arr = ['name','phone','address','details_address','province_id','city_id','area_id','xiangzhen_id'];
        $tip = ['请填写名字','请填写手机号','请填写居住地','请填写具体地址','请选择省份','请选择城市ID','请选择区域ID','请选择乡镇ID'];
        checkForData($arr,$tip,$params);
        if(self::get_address_builder_query()->create($params) === false){
            self::error('添加失败');
        }
    }

    public static function update_address(array $params)
    {
        $arr = ['name','phone','address','details_address','address_id','province_id','city_id','area_id','xiangzhen_id'];
        $tip = ['请填写名字','请填写手机号','请填写居住地','请填写具体地址','请选择地址id','请选择省份','请选择城市ID','请选择区域ID','请选择乡镇ID'];
        checkForData($arr,$tip,$params);
        if(self::get_address_builder_query(['address_id'=>self::deleteParamId($params,'address_id')])->update(self::filterFields($params,new AddressModel())) === false){
            self::error('添加失败');
        }
    }


    /**
     * @throws SysExceptions
     */
    public static function del_address($params = []){
        $arr = ['address_id'];
        $tip = ['请选择地址id'];
        checkForData($arr,$tip,$params);
        if(self::get_address_builder_query(['address_id'=>self::deleteParamId($params,'address_id')])->delete() === false){
            self::error('删除失败');
        }
    }

    public static function address(array $params)
    {
        return self::get_address_builder_query([
            'user_id'=>$params['user_id']
        ])->orderByDesc('is_default')->get();
    }

    /**
     * @throws SysExceptions
     */
    public static function set_default(array $params)
    {
        DB::beginTransaction();
        if(!checkIsHasField($params,'address_id')){
            self::error('请选择地址');
        }
        $res = self::get_address_builder_query([
            'user_id'=>$params['user_id']
        ])->update([
            'is_default'=>0
        ]);

        $res !== false && $res = self::get_address_builder_query([
            'user_id'=>$params['user_id'],
            'address_id'=>$params['address_id']
        ])->update([
            'is_default'=>1
        ]);
        if($res === false){
            DB::rollBack();
            self::error('失败了');
        }
        DB::commit();
    }

    public static function address_info(array $array)
    {
        return self::get_address_builder_query($array)->orderByDesc('is_default')->first();
    }
}
